<?php

/**
 * Created by PhpStorm.
 * User: wu
 * Date: 2017/6/16
 * Time: 9:43
 */
namespace V1;
class Article extends \ResourceClass
{
    public $isActive = true;
    public $remark = '文章接口';

    public function post(array $req, array $post, &$result)
    {
        if (!isset($req['uid'])) {
            return 'require UID';
        }

        $payload = isset($post['payload']) ? base64_decode($post['payload']) : '[]';
        $data = json_decode($payload,true);
        if (isset($data['content'])) {
            $article_content_req = json_decode($data['content']);
        } else {
            $article_content_req = array();
        }
        try {
            $article_biz = new \Biz\Article();
            $article_class = new \Orm\Article\ArticleClass();

            $article_class['uid'] = $req['uid'];
            $article_class['title'] = isset($data['title']) ? $data['title'] : '';
            $article_class['remark'] = isset($data['remark']) ? $data['remark'] : '';
            $article_class['img'] = isset($data['img']) ? $data['img'] : '';
            $article_biz->setArticle($article_class);
            foreach ($article_content_req  as $content) {
                $article_content_class = new \Orm\ArticleContent\ArticleContentClass();
                $article_content_class['type'] = $content['type'];
                $article_content_class['content'] = $content['content'];
                $article_content_class['sort'] = $content['sort'];
                $article_biz->setArticleContent($article_content_class);
            }
            $article_biz->create();
            $article_class->loadById($article_class['id']);
            $result = array(
                'id' => $article_class['id'],
                'uid' => $article_class['uid'],
                'title' => $article_class['title'],
                'img' => $article_class['img'],
                );
        }catch (\Exception $e ) {
            return $e->getMessage();
        }
        return '';
    }

    public function get(array $req, array $post, &$result)
    {
        if (!isset($req['uid'])) {
            return 'require UID';
        }
        if (!isset($req['id'])) {
            return 'require ID';
        }

        try {
            $article_class = new \Orm\Article\ArticleClass();
            $article_class->loadById($req['id']);
            $article_class->completeContent();
            $result = array(
                'id' => $article_class['id'],
                'uid' => $article_class['uid'],
                'title' => $article_class['title'],
                'img' => $article_class['img'],
                'content' => $article_class['conent_list'],
                );
        }catch (\Exception $e ) {
            return $e->getMessage();
        }
        return '';
    }

    public function put(array $req, array $post, &$result)
    {
        if (!isset($req['uid'])) {
            return 'require UID';
        }

        $data = base64_decode($post['payload']);
        @parse_str($data, $data);
        try {
            $article_class = new \Orm\Article\ArticleClass();
            $article_class->loadById($req['id']);
            if(isset($data['title'])) {
                $article_class['title'] = $data['title'];
            }
            if(isset($data['remark'])) {
                $article_class['remark'] = $data['remark'];
            }
            if(isset($data['status'])) {
                $article_class['status'] = $data['status'];
            }
            $result = $article_class->objToArray($article_class);

            $article_class->update();
            $result = array(
                'id' => $article_class['id'],
                'uid' => $article_class['uid'],
                'title' => $article_class['title'],
                'img' => $article_class['img'],
                'status' => $article_class['status'],
            );
        }catch (\Exception $e ) {
            return $e->getMessage();
        }
        return '';
    }
}